package com.hs.telepass.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    private static SQLiteDatabase mDb;
    private static DatabaseHelper mHelper;
    private static final int DB_VERSION = 38;
    private static final String DB_NAME = "main";

    public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public DatabaseHelper(Context context) {
        this(context, DB_NAME, null, DB_VERSION);
    }

    public static SQLiteDatabase getInstance(Context context) {
        if (mDb == null) {
            mDb = getHelper(context).getWritableDatabase();
        }
        return mDb;
    }

    public static DatabaseHelper getHelper(Context context) {
        if (mHelper == null) {
            mHelper = new DatabaseHelper(context);
        }
        return mHelper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table " + TelepassProfilesDatabase.TABLE_NAME
                + " (" + TelepassProfilesDatabase.COLUMN_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + TelepassProfilesDatabase.COLUMN_HOST + " TEXT, "
                + TelepassProfilesDatabase.COLUMN_PATH + " TEXT, "
                + TelepassProfilesDatabase.COLUMN_USERNAME + " TEXT, "
                + TelepassProfilesDatabase.COLUMN_ENCRYPTED_PASSWORD + " TEXT)");

        db.execSQL("create table " + SingouProfilesDatabase.TABLE_NAME
                + " (" + SingouProfilesDatabase.COLUMN_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + SingouProfilesDatabase.COLUMN_SERVICE_ID + " TEXT, "
                + SingouProfilesDatabase.COLUMN_SERVICE_NAME + " TEXT, "
                + SingouProfilesDatabase.COLUMN_TITLE + " TEXT, "
                + SingouProfilesDatabase.COLUMN_LOGO_URL + " TEXT, "
                + SingouProfilesDatabase.COLUMN_USERNAME + " TEXT, "
                + SingouProfilesDatabase.COLUMN_ENCRYPTED_PASSWORD + " TEXT)");

        db.execSQL("create table " + RepositoryProfilesDatabase.TABLE_NAME
                + " (" + RepositoryProfilesDatabase.COLUMN_KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + RepositoryProfilesDatabase.COLUMN_SERVICE_ID + " TEXT, "
                + RepositoryProfilesDatabase.COLUMN_SERVICE_NAME + " TEXT, "
                + RepositoryProfilesDatabase.COLUMN_TITLE + " TEXT, "
                + RepositoryProfilesDatabase.COLUMN_VERSION + " TEXT, "
                + RepositoryProfilesDatabase.COLUMN_LOGOURL + " TEXT, "
                + RepositoryProfilesDatabase.COLUMN_INFOURL + " TEXT, "
                + RepositoryProfilesDatabase.COLUMN_KEYURL + " TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            clearAllTable(db);
        }
    }


    public void clearAllTable(SQLiteDatabase db) {
        db.execSQL("DROP TABLE IF EXISTS " + TelepassProfilesDatabase.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + SingouProfilesDatabase.TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + RepositoryProfilesDatabase.TABLE_NAME);
        onCreate(db);
    }
}
